# coding=utf-8
import json
import time
import pymysql
from config import db_host, db_port
import redis
from playwright.sync_api import sync_playwright
import datetime
db_host = '121.4.69.146'
db_port = 3306

# redis_con = redis.Redis(host='121.4.69.146', password='xhhg123456',
#                         port=6389, db=2, decode_responses=True)     # 连接redis数据库

redis_con = redis.Redis(host='127.0.0.1', port=6379,
                        db=2, decode_responses=True)
now_date = datetime.date.today()
month = now_date.month
if month < 10:
    month = f'0{month}'
day = now_date.day
if day < 10:
    day = f'0{day}'
now_time = f'{month}{day}'
# now_time = '0809'


def parse_patent_info(application_number, patent_status_res_json, inst):
    # 解析获取专利信息
    res_msg = patent_status_res_json.get('msg')
    if res_msg == '成功':
        patent_status_data = patent_status_res_json.get('data')
        item = dict()
        item['sqh'] = 'ZL' + application_number
        try:
            item["state"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('anjianywzt')
        except:
            item["state"] = ''
        try:
            item["name"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('zhuanlimc')
        except:
            item["name"] = ''
        try:
            item["sq_date"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('shenqingr')
        except:
            item["sq_date"] = ''
        try:
            dljg_info = patent_status_data.get(
                'dailijg').get('dailijgList')[-1]
            item["dljg"] = dljg_info.get('dailijgdm')
            item["dlr"] = dljg_info.get('diyidlrxm')
        except:
            item["dljg"] = ''
            item["dlr"] = ''
        famingrenList = patent_status_data.get(
            'famingren').get('famingrenList')
        try:
            sqr_list = [line.get('famingrxm') for line in famingrenList]
        except:
            sqr_list = []
        try:
            shenqingrenList = patent_status_data.get(
                'shenqingren').get('shenqingrenList')
            item["sqr"] = ','.join([line.get('shenqingrxm')
                                    for line in shenqingrenList])
        except:
            item["sqr"] = ''
        item["fmr_name"] = ','.join(sqr_list)
        item['inst'] = inst
        try:
            item['fm_class'] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('zhufenlh')
        except:
            item['fm_class'] = ''
        print(item)
        print('====' * 10)
        if inst == 'XHHG' and item["name"] == '--':
            try:
                redis_con.sadd("协和端口查询", application_number)
            except:
                time.sleep(5)
                redis_con.sadd("协和端口查询", application_number)
            # 使用协和端口查询
            print('@@xhhg未查到相关信息，使用xh进行查询@@')
        elif inst == 'XH' and item["name"] == '--':
            try:
                redis_con.sadd("外部接口查询", application_number)
            except:
                time.sleep(5)
                redis_con.sadd("外部接口查询", application_number)
            print('@@xh端口未查到相关信息，使用外部接口查询@@')
        elif inst == 'WB' and item["name"] == '--':
            try:
                redis_con.sadd("全部接口未查到", application_number)
            except:
                time.sleep(5)
                redis_con.sadd("全部接口未查到", application_number)
            print('@@全部接口均未查到@@')
        if item["name"] != '--' or inst == 'WB':
            for k, v in item.items():
                if v == '--':
                    item[k] = ''
            global now_time
            sql = f"""INSERT INTO xhhg_pantent_status_{now_time} (application_number, case_status, fm_name,fm_class, sq_date, created_at, updated_at, agency, patent_user, first_agent, fmr, port) VALUES
                ('{item["sqh"]}', '{item["state"]}', '{item["name"]}', '{item['fm_class']}', '{item["sq_date"]}', NOW(), NOW(), '{item["dljg"]}', '{item["sqr"]}', '{item["dlr"]}', '{item["fmr_name"]}', '{item['inst']}')"""
            print(sql)
            db = pymysql.connect(host=db_host,
                                 user='root',
                                 port=db_port,
                                 password='xhhg@2020',
                                 database='big_data',
                                 connect_timeout=120)
            cur = db.cursor()
            try:
                cur.execute(sql)
                db.commit()
            except Exception as e:
                print(e)
                print(f'{application_number}_插入数据库失败，5分钟后重试')
                time.sleep(60)
                cur.execute(sql)
                db.commit()
            finally:
                db.close()
                cur.close()


def select_sqh(now_time):
    db = pymysql.connect(host=db_host,
                         user='root',
                         port=db_port,
                         password='xhhg@2020',
                         database='big_data')
    cur = db.cursor()
    sql = f'SELECT DISTINCT application_number FROM xhhg_pantent_status_{now_time}'
    cur.execute(sql)
    data = cur.fetchall()
    have_already_done = set([sqh[0].replace("ZL", '') for sqh in data])
    db.close()
    cur.close()
    return have_already_done


def run(i, inst, port, have_already_done):
    all_start_time = time.time()
    with sync_playwright() as p:
        storage_state_path = f'./auth/{inst}_state.json'
        with open(storage_state_path) as file:
            storage_state = json.load(file)
            origins = storage_state.get('origins')
            local_stroge_list = [item.get('localStorage') for item in origins]
            for local_stroge in local_stroge_list:
                for item in local_stroge:
                    if item.get('name') == 'ACCESS_TOKEN':
                        authorization = item.get('value')
        print(f'成功读取{inst}登录信息:{authorization}')
        browser = p.firefox.launch()
        context = browser.new_context(
            base_url='https://cpquery.cponline.cnipa.gov.cn', storage_state=storage_state_path)
        context.set_default_timeout(600000)
        context.set_default_navigation_timeout(600000)
        page = context.new_page()
        page.set_default_timeout(600000)
        page.set_default_navigation_timeout(600000)
        page.goto(
            'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index')
        page.wait_for_load_state('networkidle')
        scraped_nums = 1
        while redis_con.scard(port):
            sqh = redis_con.spop(port).replace("ZL", '')
            scraped_nums += 1
            if sqh not in have_already_done:
                start_time = time.time()
                print(f'@@开始查询申请号:{sqh}_专利状态信息@@')
                api_request_context = context.request
                headers = {
                    "Accept": "application/json, text/plain, */*",
                    "Content-Type": "application/json;charset=utf-8",
                    "Authorization": 'Bearer ' + authorization,
                    "userType": "USER_RZ_DAILIJIG"
                }
                try:
                    sqxx_response = api_request_context.post(
                        '/api/view/gn/sqxx', data={'zhuanlisqh': sqh}, headers=headers, timeout=0)
                    patent_status_res_json = sqxx_response.json()
                    parse_patent_info(sqh, patent_status_res_json, inst)
                    end_time = time.time()
                    print(f"{sqh}_本次耗时: {end_time-start_time}")
                    need_time = round((end_time - start_time) *
                                      redis_con.scard(port) / 60, 1)
                    print(
                        f"进程{i}_已抓取专利数：{scraped_nums}，剩余{redis_con.scard(port)}个，大约还需要{need_time}分钟")
                except:
                    redis_con.sadd(port, sqh)
                    print(f'{sqh}_查询超时，重试')
                    continue
                page.reload()
                # page.wait_for_load_state('networkidle')
    all_end_time = time.time()
    print(f"@@总耗时：{all_end_time-all_start_time}@@")


def create_table(now_time):
    # redis创建需查询的专利号
    create_sql_payment = f"""
    CREATE TABLE If Not Exists `big_data`.`xhhg_pantent_status_{now_time}`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `application_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `case_status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `fm_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `fm_class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `sq_date` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `created_at` timestamp(0) NULL DEFAULT NULL,
  `updated_at` timestamp(0) NULL DEFAULT NULL,
  `priority_reviewl` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `agency` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `is_ours_cases` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `patent_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `patent_user` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `first_agent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `port` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `fmr` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"""
    db = pymysql.connect(host=db_host,
                         user='root',
                         port=db_port,
                         password='xhhg@2020',
                         database='big_data')
    cur = db.cursor()
    cur.execute(create_sql_payment)
    db.commit()
    db.close()
    cur.close()
    print(f'xhhg_pantent_status_{now_time}_创建成功')


if __name__ == '__main__':
    create_table(now_time)  # 生成目标表
    have_already_done = select_sqh(now_time)
    print('@@获取已抓取数据@@')
    port_dict = {'XHHG': '信合红谷接口查询', 'XH': '协和端口查询', 'WB': "外部接口查询"}
    inst = 'XHHG'
    port = port_dict.get(inst)
    run(0,inst, port, have_already_done)
    # from multiprocessing.pool import Pool
    # p = Pool(5)
    # for i in range(5):
    #     p.apply_async(run, args=(i, inst, port, have_already_done))
    # p.close()
    # p.join()
